{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "af9371cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os \n",
    "\n",
    "import datetime "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "558a0935",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"\"\"D:/Gitee/jupyter/competitions/kaggle/titanic/datasets\"\"\"\n",
    "\n",
    "file_1 = \"gender_submission.csv\"\n",
    "file_train = \"train.csv\"\n",
    "file_test = \"test.csv\"\n",
    "\n",
    "print(datetime.date.today().strftime(\"%Y%m%d\"))\n",
    "subfilename = os.path.join(path,\"submission_%s.csv\" % datetime.date.today().strftime(\"%Y%m%d\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "94657e1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.read_csv(os.path.join(path, file_train))\n",
    "df_test = pd.read_csv(os.path.join(path, file_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "0a85489c",
   "metadata": {},
   "outputs": [],
   "source": [
    "features = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\",\"Fare\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d43c8864",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = df_train[features]\n",
    "test_data = df_test[features]\n",
    "\n",
    "\n",
    "#train_data = df_train\n",
    "#test_data = df_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "6cfe4e41",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ldq\\AppData\\Local\\Temp\\ipykernel_49376\\4063273702.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  test_data['Fare'].fillna(test_data['Fare'].mean(), inplace=True)\n"
     ]
    }
   ],
   "source": [
    "test_data['Fare'].fillna(test_data['Fare'].mean(), inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "e00f0353",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "y = df_train[\"Survived\"]\n",
    "\n",
    "X = pd.get_dummies(train_data)\n",
    "X_test = pd.get_dummies(test_data)\n",
    "\n",
    "model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)\n",
    "model.fit(X, y)\n",
    "predictions = model.predict(X_test)\n",
    "\n",
    "output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': predictions})\n",
    "#output.to_csv('submission.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "4ee170ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "output.to_csv(subfilename, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "7482aa82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20220518\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "f14763f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'submission_{20220518}'"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
